package com.qian.fmall.order.wconfig;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;


@Component
@ServerEndpoint("/webSocket/{oid}")
public class WenSocketServer {
    private static ConcurrentHashMap<String, Session> sessionMap=new ConcurrentHashMap<>();

    @OnOpen
    public void  open(@PathParam("oid") String orderId,Session session)
    {
        sessionMap.put(orderId, session);
    }

    @OnClose
    public void close(@PathParam("oid") String orderId)
    {
        sessionMap.remove(orderId);
    }

    public  static void sendMessage(String orderId,String msg)
    {
        Session se = sessionMap.get(orderId);
        System.out.println("se--"+se);
        try {
            se.getBasicRemote().sendText(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
